
<%- include("include/header", {"title":"登录", "constant":constant})%>
  <body class="login-body">

    <div class="container">

      <form class="form-signin" action="/ajax/checklogin" method="post">
        <h2 class="form-signin-heading">管理员登录</h2>
        <div class="login-wrap">
            <input type="text" name="username" id="username" class="form-control" autocomplete="on" placeholder="用户名" autofocus/>
            <input type="password" name="password" id="password" class="form-control" autocomplete="off" placeholder="密码" />
           <!-- <label class="checkbox">
                登录状态将保持2天
                <span class="pull-right"> <a href="#"> 忘记密码?</a></span>
            </label>
			-->
            <input class="btn btn-lg btn-login btn-block" type="button" value="登录" onclick="login()" />
        </div>
		<div id="errorcontainer">
	  	</div>
      </form>
	  
    </div>

	<script type="text/javascript">
		String.prototype.format = function(args){
			var result = this;

			if(arguments.length>0){

				if(arguments.length ==1 && typeof (args)=="object"){
					for(var key in args){
						if(args[key] !=undefined){
							var reg = new RegExp("({"+key+"})","g");
							result = result.replace(reg,args[key]);
						}
					}
				}else{
					for(var i = 0; i<arguments.length;i++){
						if(arguments[i] != undefined){
							var reg = new RegExp("({)"+i+"(})","g");
							result = result.replace(reg,arguments[i]);
						}
					}
				}
			}

			return result;
		}
		$(function(){
			document.onkeydown = function(ev){
				var ev = ev || window.event;
				if(ev && ev.keyCode == 13){
					login();
				}
			}
		
		});
		function login(){
			var UserName = $("#username"),
				UserNameVal = $.trim(UserName.val()),
				Password = $("#password"),
				PasswordVal = $.trim(Password.val()),
				errorTip = '<div id="errorTip" class="alert alert-warning">{0}</div>';
			$("#errorTip,#alt_warning").remove();
			var ret = checkUsername(UserNameVal);
			if(!ret.success){
				$("#errorcontainer").prepend(errorTip.format(ret.error));
				return false;
			}
			ret = checkPassword(PasswordVal);
			if(!ret.success){
				$("#errorcontainer").prepend(errorTip.format(ret.error));
				return false;
			}
			this.loginby(UserNameVal, PasswordVal);
			return true;
		}
		function loginby(user,pwd){
			var postdata = {username:user,password:pwd}
			var errorTip = '<div id="errorTip" class="alert alert-warning">{0}</div>';
			$.post('/logincheck',postdata,function(data){
				if(data.success){
					window.location.href='index.html';
				}else{
					$("#errorcontainer").prepend(errorTip.format(data.error));
				}
			})
		}
		function checkUsername(username){	
			if(username.length <= 0){
				return makeErrorReturn('用户名不能为空');
			}
			var regUser1 = new RegExp("^[A-Z0-9a-z]{1,}$");
			var regUser2 = new RegExp("^[\u4e00-\u9fa5]{1,}$");
			if(!regUser1.test(username) && !regUser2.test(username)){
				return makeErrorReturn("用户名只能使用中文,英文和数字");
			}
			return {"success":true};
		}
		function checkPassword(password){
			if(password.length <= 0){
				return makeErrorReturn('密码不能为空');
			}
			var regPwd = new RegExp("^[A-Z0-9a-z]{6,}$");
			if(!regPwd.test(password)){
				return makeErrorReturn("密码只能使用英文和数字，最少6个字符");
			}
			return {"success":true};
		}
		function makeErrorReturn(msg){
			return {"error":msg, "success":false}
		}
	</script>
  </body>
</html>
